package com.hngates.architecture.controller;

import com.hngates.architecture.domain.ArcBuilderCompany;
import com.hngates.architecture.domain.ArcConstructContract;
import com.hngates.architecture.domain.ArcLabourCompany;
import com.hngates.architecture.domain.ArcProject;
import com.hngates.architecture.service.IArcBuilderCompanyService;
import com.hngates.architecture.service.IArcConstructContractService;
import com.hngates.architecture.service.IArcLabourCompanyService;
import com.hngates.architecture.service.IArcProjectService;
import com.hngates.common.annotation.Log;
import com.hngates.common.constant.Constants;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.core.page.TableDataInfo;
import com.hngates.common.enums.BusinessType;
import com.hngates.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 施工合同Controller
 *
 * @author jwhe
 * @date 2022-07-04
 */
@Controller
@RequestMapping("/architecture/constructContract")
public class ArcConstructContractController extends BaseController {
    private String prefix = "architecture/constructContract";

    @Autowired
    private IArcConstructContractService arcConstructContractService;

    @Autowired
    private IArcProjectService arcProjectService;

    @Autowired
    private IArcBuilderCompanyService arcBuilderCompanyService;

    @Autowired
    private IArcLabourCompanyService arcLabourCompanyService;

    @RequiresPermissions("architecture:constructContract:view")
    @GetMapping()
    public String constructContract(ModelMap modelMap) {
        ArcProject arcProject = new ArcProject();
        arcProject.setDelFlag(Constants.DEl_FLAG_0);
        List<ArcProject> arcProjects = arcProjectService.selectArcProjectList(arcProject);

        ArcBuilderCompany query2 = new ArcBuilderCompany();
        query2.setDelFlag(Constants.DEl_FLAG_0);
        List<ArcBuilderCompany> arcBuilderCompanies = arcBuilderCompanyService.selectArcBuilderCompanyList(query2);


        query2.setDelFlag(Constants.DEl_FLAG_0);
        ArcLabourCompany arcLabourCompany = new ArcLabourCompany();
        arcLabourCompany.setDelFlag(Constants.DEl_FLAG_0);
        List<ArcLabourCompany> arcLabourCompanies = arcLabourCompanyService.selectArcLabourCompanyList(arcLabourCompany);


        modelMap.put("projects", arcProjects);
        modelMap.put("builders", arcBuilderCompanies);
        modelMap.put("labours", arcLabourCompanies);


        return prefix + "/constructContract";
    }

    /**
     * 查询施工合同列表
     */
    @RequiresPermissions("architecture:constructContract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ArcConstructContract arcConstructContract) {
        startPage();
        List<ArcConstructContract> list = arcConstructContractService.selectArcConstructContractList(arcConstructContract);
        return getDataTable(list);
    }

    /**
     * 导出施工合同列表
     */
    @RequiresPermissions("architecture:constructContract:export")
    @Log(title = "施工合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ArcConstructContract arcConstructContract) {
        List<ArcConstructContract> list = arcConstructContractService.selectArcConstructContractList(arcConstructContract);
        ExcelUtil<ArcConstructContract> util = new ExcelUtil<ArcConstructContract>(ArcConstructContract.class);
        return util.exportExcel(list, "施工合同数据");
    }

    /**
     * 新增施工合同
     *
     * @param modelMap 用于存储给页面推送的数据
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        //定义查询对象
        ArcProject projectQuery = new ArcProject();
        //设置查询参数，只查未做删除标记的记录
        projectQuery.setDelFlag(Constants.DEl_FLAG_0);
        //执行查询，将结果放至arcProjects对象中
        List<ArcProject> arcProjects = arcProjectService.selectArcProjectList(projectQuery);
        //将arcProjects变量放置页面中，此时页面能够拿到一个对象名为projects的对象
        modelMap.put("projects", arcProjects);

        return prefix + "/add";
    }

    /**
     * 新增保存施工合同
     */
    @RequiresPermissions("architecture:constructContract:add")
    @Log(title = "施工合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ArcConstructContract arcConstructContract) {
        return toAjax(arcConstructContractService.insertArcConstructContract(arcConstructContract));
    }

    /**
     * 修改施工合同
     */
    @RequiresPermissions("architecture:constructContract:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        //定义查询对象
        ArcProject projectQuery = new ArcProject();
        //设置查询参数，只查未做删除标记的记录
        projectQuery.setDelFlag(Constants.DEl_FLAG_0);
        //执行查询，将结果放至arcProjects对象中
        List<ArcProject> arcProjects = arcProjectService.selectArcProjectList(projectQuery);
        //将arcProjects变量放置页面中，此时页面能够拿到一个对象名为projects的对象
        mmap.put("projects", arcProjects);

        ArcConstructContract arcConstructContract = arcConstructContractService.selectArcConstructContractById(id);
        mmap.put("arcConstructContract", arcConstructContract);
        return prefix + "/edit";
    }

    /**
     * 修改保存施工合同
     */
    @RequiresPermissions("architecture:constructContract:edit")
    @Log(title = "施工合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ArcConstructContract arcConstructContract) {
        return toAjax(arcConstructContractService.updateArcConstructContract(arcConstructContract));
    }

    /**
     * 删除施工合同
     */
    @RequiresPermissions("architecture:constructContract:remove")
    @Log(title = "施工合同", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(arcConstructContractService.deleteArcConstructContractByIds(ids));
    }
}
